package org.imlinker.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import org.imlinker.LocalVariablesManager;
import org.imlinker.buddy.BuddyChat;

/**
 * Chat Window,display chat information,to send message.
 *  
 */

public class ChatWindow extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The area which used to display the chat information.
	 */
	private JTextArea chatArea;

	/**
	 * The buddy's chatCell.
	 */
	private BuddyChat chatCell;

	/**
	 * The textField used to input message.
	 */
	private final JTextField chatIn;

	/**
	 * The constructer of ChatWindow.
	 * 
	 * @param buddyChat
	 *            the ChatCell linked to this chatWindow
	 */
	public ChatWindow(final BuddyChat buddyChat) {
		super("Link to " + (buddyChat.getUser()));
		setName(buddyChat.getUser());
		setTitle("Link to " + buddyChat.getName());

		this.chatCell = buddyChat;

		JMenuBar bar = new JMenuBar();
		JMenu menu = new JMenu(LocalVariablesManager
				.getLocalVariables("ChatWindow.SettingMenu"));
		JMenuItem item = new JMenuItem(LocalVariablesManager
				.getLocalVariables("ChatWindow.SettingMenu.Info"));
		item.addActionListener(this);
		menu.add(item);
		bar.add(menu);
		setJMenuBar(bar);

		Container container = getContentPane();

		chatArea = new JTextArea(buddyChat.getChatLog());
		chatArea.setEditable(false);
		chatArea.setLineWrap(true);
		container.add(new JScrollPane(chatArea), BorderLayout.CENTER);

		chatIn = new JTextField();
		chatIn.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				buddyChat.send(e.getActionCommand());
				chatIn.setText("");
			}
		});
		container.add(chatIn, BorderLayout.SOUTH);

		setSize(300, 300);
		setLocationRelativeTo(null);
		try {
			WindowSaver.loadSettings(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		showWindow();
	}

	/**
	 * Display the message on chat Area.
	 * 
	 * @param message
	 *            the message to display
	 */
	public final void display(final String message) {
		if (message == null) {
			return;
		}

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (chatArea.getText().length() > 0 && message.length() > 0) {
					chatArea.append("\n");
				}
				chatArea.append(message);
				chatArea.setCaretPosition(chatArea.getText().length());
			}
		});
	}

	/**
	 * The actionPreformed used to catch actionEvent.
	 * 
	 * @param e
	 *            ActionEvent.
	 */
	public final void actionPerformed(final ActionEvent e) {
		if (e.getActionCommand() == LocalVariablesManager
				.getLocalVariables("ChatWindow.SettingMenu.Info")) {
			new BuddyInfoWindow(chatCell);
		}
	}

	/**
	 * Show window and get focus point to the edit textfield.
	 */
	public final void showWindow() {
		setVisible(true);
		chatIn.grabFocus();
	}

}
